<?php

namespace Common\Model;

/**
 * 父订单修改记录表
 * @modify trainyao 2016-01-05 创建
 * @index orderinfo_id (`orderinfo_id`);
 */
Class OrderRecordModel extends \Common\Model\BaseModel {

    protected $trueTableName = 'tb_order_record';

    protected function _before_insert(&$data, $options) {
        if (!isset($data['creator'])) {
            $data['creator'] = getUserId();
        }
    }

    protected function _before_update(&$data, $options) {
        
    }

    /**
     * 取父订单修改记录
     * @modify trainyao 2016-01-05 创建
     * @param  integer   $orderId   父订单id
     * @return array              修改记录数组
     */
    public function getInfoByOrder($orderId) {
        $option = array(
            'orderinfo_id' => $orderId,
        );
        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 添加订单记录
     * @modify songanwei 2015-01-06
     * @modify wenhui 2017-10-11 添加 字段 $orderPayRecord
     * @param integer $orderId 订单ID
     * @param integer $recordFrom 操作来源
     * @param inteegr $payment 应付金额
     * @param integer $order   订单数据变化
     * @param integer $user  用户数据是否有变化
     * @param integer $room  房间操作是否有变化
     * @param integer $orderPayRecord  订单支付记录变化
     * @param integer $checkRoom  查房是否有变化
     * @return type
     */
    public function addInfo($orderId, $recordFrom, $payment = null, $order = null, $user = null, $room = null, $creator = null, $orderPayRecord = null, $checkRoom = null) {
        $data = array(
            'orderinfo_id' => $orderId,
            'recordfrom' => $recordFrom,
        );
        if (!empty($payment)) {
            $data['payment'] = $payment;
        }
        if (!empty($order)) {
            $data['order'] = $order;
        }
        if (!empty($user)) {
            $data['user'] = $user;
        }
        if (!empty($room)) {
            $data['room'] = $room;
        }
        if (!is_null($creator)) {
            $data['creator'] = $creator;
        }
        if (!is_null($orderPayRecord)) {
            $data['orderpayrecord'] = $orderPayRecord;
        }
        if (!is_null($checkRoom)) {
            $data['check'] = $checkRoom;
        }
 
        $data['createtime'] = date('Y-m-d H:i:s');

        return $this->add($data);
    }

    /**
     * 更改订单记录的父订单id
     * @modify trainyao 2016-05-19 创建
     * @param  integer   $id        条目id
     * @param  integer   $orderId   父订单ID
     * @return boolean              执行结果
     */
    public function saveOrder($id, $orderId) {
        $option = array(
            'id' => $id,
        );
        $data = array(
            'orderinfo_id' => $orderId,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据订单操作记录查询支付金额变化
     * @modify wenhui 2017-10-11      创建
     * @param  mixin   $id 订单操作记录id(s)
     */
    public function getOrderRecordByorderId($orderId) {
        $option = array(
            'orderinfo_id' => $orderId,
        );
        $data = $this->where($option)->find();

        return empty($data) ? array() : $data;
    }
}
